<?php
header("Content-type: image/x-png");
$string = isset($_GET['text'])?$_GET['text']: "deeflow.com";

$imLogo = @imagecreatefromgif("logo2.gif")
        or die("Cannot Initialize new GD image stream");
$font = (isset($_GET['font']) && $_GET['font']<=4 && $_GET['font']>=1)?$_GET['font']:1;

$fontsArray = array(
    1 => array('fileName'=>"./font1.ttf", 'kWidth'=>4.6, 'kHeigth'=>20, 'size'=>12, 'hOffset'=>18),
    2 => array('fileName'=>"./font2.ttf", 'kWidth'=>4.6  ,'kHeigth'=>20, 'size'=>12, 'hOffset'=>15),
    3 => array('fileName'=>"./font3.ttf", 'kWidth'=>5.5,  'kHeigth'=>36, 'size'=>22, 'hOffset'=>25),
    4 => array('fileName'=>"./font4.ttf", 'kWidth'=>4,    'kHeigth'=>28, 'size'=>18, 'hOffset'=>22)
);

$imageX = 320;



$maxColorWeigth = 255;
$minColorWeigth = 200;

$minRadius = 30;
$maxRadius = 50;

$f = $fontsArray[$font];
$hOffset = $f['hOffset'];
//$fontSize = imageloadfont($f['fileName']);


$maxLength = ($imageX / $f['kWidth']) - 2;

preg_match_all("{[\dА-Яа-ярстуфхцчшщьыъэю\—\:\-,\.\!\?\%]+}", $string, $keywords);
    $strArray = array();
    $iterator = 0;
    $currentStr = "";
    foreach ($keywords[0] as $word) {
        if ((strlen($currentStr) + strlen($word)-1) < $maxLength) {
            $currentStr.=' ' . $word;
        } else {
            $iterator++;
            $currentStr = $word;
        }
        $strArray[$iterator] = $currentStr;
    }
    $string = implode("\n ", $strArray);
    
    $countRows = count($strArray);



function create_watermark($main_img_obj, $watermark_img_obj, $alpha_level = 100) {
    $watermark_width = imagesx($watermark_img_obj);
    $watermark_height = imagesy($watermark_img_obj);
    $dest_x = imagesx($main_img_obj) - $watermark_width - 5;
    $dest_y = imagesy($main_img_obj) - $watermark_height - 5;
    imagesavealpha($main_img_obj, true);
    imagesavealpha($watermark_img_obj, true);
    imagecopymerge($main_img_obj, $watermark_img_obj, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $alpha_level);
    return $main_img_obj;
}

function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1) {
    /* this way it works well only for orthogonal lines
      imagesetthickness($image, $thick);
      return imageline($image, $x1, $y1, $x2, $y2, $color);
     */
    if ($thick == 1) {
        return imageline($image, $x1, $y1, $x2, $y2, $color);
    }
    $t = $thick / 2 - 0.5;
    if ($x1 == $x2 || $y1 == $y2) {
        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
    $a = $t / sqrt(1 + pow($k, 2));
    $points = array(
        round($x1 - (1 + $k) * $a), round($y1 + (1 - $k) * $a),
        round($x1 - (1 - $k) * $a), round($y1 - (1 + $k) * $a),
        round($x2 + (1 + $k) * $a), round($y2 - (1 - $k) * $a),
        round($x2 + (1 - $k) * $a), round($y2 + (1 + $k) * $a),
    );
    imagefilledpolygon($image, $points, 4, $color);
    return imagepolygon($image, $points, 4, $color);
}

function fillCircles($im, $count, $minRadius=0, $maxRadius=20, $minColorWeigth=0, $maxColorWeigth=255) {
    /*
     * $im - image Object
     * $count - quantity of circles in this image
     * $minRadius - minimum radius of each circle
     * $maxRadius - maximum radius of each circle
     * $minColorWeigth, $maxColorWeigth - color range
     */
    
    
    for ($i = 0; $i < $count; $i++) {
//            $radius = rand($minRadius, $maxRadius);
        $radius = rand($maxRadius-ceil($maxRadius*0.77), $maxRadius);
        $color = imagecolorallocate($im, rand($minColorWeigth, $maxColorWeigth), rand($minColorWeigth, $maxColorWeigth), rand($minColorWeigth, $maxColorWeigth));
        $color = imagecolorallocate($im, rand($minColorWeigth, $maxColorWeigth), rand($minColorWeigth, $maxColorWeigth), rand($minColorWeigth, $maxColorWeigth));
        imagefilledellipse($im, rand(-$radius, imagesx($im)+$radius), rand(-$radius, imagesy($im)+$radius), $radius, $radius, $color);
    }
    
    return $im;
}

function fillLines($im, $upFreq=2, $downFreq=6, $params=array()) {
    
    
    $count = ceil(imagesx($im) / $upFreq);
    $color = imagecolorallocate($im, 240, 240, 240);

    for ($i = 1; $i < $count; $i++) {
        imageline($im, $i * $upFreq, 0, $i * $downFreq, imagesy($im), $color);
        imageline($im, imagesx($im) - $i * $upFreq, 0, imagesx($im) - $i * $downFreq, imagesy($im), $color);
        //$im = fillLines($im, 7, 5);
        //imageline($im, imagesx($im) - $i * $upFreq, 0, imagesx($im) - $i * $downFreq, imagesy($im), $color);
    }

    return $im;
}


# --------- Global


$imageY = $countRows*$f['kHeigth']+8; 

$im = imagecreatetruecolor($imageX, $imageY);
$backGroundColor = imagecolorallocate($im, 255, 255, 255);
$textColor = imagecolorallocate($im, 0, 0, 0);

imagefill($im, 0, 0, $backGroundColor);

    
$im = fillCircles($im, 100, $minRadius, $maxRadius, $minColorWeigth, $maxColorWeigth);

if($countRows>2){
    $im = fillLines($im, 5, 10, array('fv'));
}

//imagettftext($im, imagefontwidth($font['fileName'])*2.6 , 0, 5, 25, $textColor, $font['fileName'], imagefontwidth($font['fileName']).$string);
imagettftext($im, $f['size'], 0, 0, $hOffset, $textColor, $f['fileName'], $string);

$im3 = create_watermark($im, $imLogo, 20);



imagepng($im3);
imagedestroy($im3);
?>

<style>
    body{
        font-family: verdana;
    }
</style>